﻿/*---------------------------------------------------------------------*/
// fileName="XMLHelper.cs" CompanyName="SureKAM"
// Copyright SureKAM (c). All right reserved.
/*---------------------------------------------------------------------*/

using System.Data;
using System.Reflection;
using System.Xml;

namespace SureKAM.BackUp.Notify.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// XML操作类
    /// </summary>
    public class XMLHelper
    {
        #region 私有字段
        private string fileName;
        private XmlDocument xmlDocument;
        #endregion

        #region 构造
        /// <summary>
        /// XMLHelper构造函数
        /// </summary>
        /// <param name="fileName"></param>
        public XMLHelper(string fileName)
        {
            this.fileName = fileName;
            this.xmlDocument = new XmlDocument();
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 打开相应的XML文件
        /// </summary>
        private void Open()
        {
            if (string.IsNullOrEmpty(this.fileName)) throw new ArgumentNullException("Error:100001");
            this.xmlDocument.Load(this.fileName);
        }

        /// <summary>
        /// 保存相应的XML文件
        /// </summary>
        private void Save()
        {
            try
            {
                if (string.IsNullOrEmpty(this.fileName) || this.xmlDocument == null) throw new ArgumentNullException("Error:100002");
                this.xmlDocument.Save(this.fileName);
            }
            catch (Exception)
            {
                
               
            }
          
        }
        #endregion

        #region 属性
        /// <summary>
        /// XML文件路径
        /// </summary>
        public string FileName
        {
            get { return this.fileName; }
            set { this.fileName = value; }
        }
        #endregion

        #region  公有方法
        /// <summary>
        /// 获取所有的数据
        /// </summary>
        /// <returns></returns>
        public List<T> GetNodes<T>()
        {
            XmlNodeList xmlNodeList = null;
            List<T> list = new List<T>();
            try
            {
                this.Open();
                xmlNodeList = this.xmlDocument.SelectSingleNode("DocumentValue").ChildNodes;
                Type type = typeof(T);
                foreach (XmlNode singleXmlNode in xmlNodeList)
                {
                    T singleElement = (T)type.Assembly.CreateInstance(type.FullName);
                    XmlElement xmlElement = (XmlElement)singleXmlNode;
                    if (xmlElement.Name == type.Name)
                    {
                        foreach (XmlNode childNode in xmlElement.ChildNodes)
                        {
                            ////利用反射给相应的属性赋值
                            try
                            {
                                if (!string.IsNullOrEmpty(childNode.InnerText))
                                    singleElement.GetType().GetProperty(childNode.Name).SetValue(singleElement, Convert.ChangeType(childNode.InnerText, singleElement.GetType().GetProperty(childNode.Name).PropertyType), null);
                            }
                            catch (Exception)
                            {
                                singleElement.GetType().GetProperty(childNode.Name).SetValue(singleElement, Convert.ChangeType(null, singleElement.GetType().GetProperty(childNode.Name).PropertyType), null);
                            }
                        }
                    }
                    list.Add(singleElement);
                }
            }
            catch (Exception)
            {
                list = null;
                this.Save();
                throw new ArgumentNullException("Error:100003");
            }
            this.Save();
            return list;
        }

        /// <summary>
        /// 保存XML文件
        /// </summary>
        /// <param name="xmlNodeListm"></param>
        /// <returns></returns>
        public bool SaveToFile<T>(List<T> list)
        {
            bool opreation = false;
            try
            {
                this.Open();
                XmlNode root = this.xmlDocument.SelectSingleNode("DocumentValue");
                root.RemoveAll();
                foreach (var o in list)
                {
                    if (o.GetType().IsClass)
                    {
                        XmlElement xmlElement = this.xmlDocument.CreateElement(o.GetType().Name);
                        PropertyInfo[] propertyInfos = o.GetType().GetProperties();
                        foreach (PropertyInfo propertyInfo in propertyInfos)
                        {
                            try
                            {
                                xmlElement.AppendChild(this.xmlDocument.CreateElement(propertyInfo.Name)).InnerText =
                                    ((propertyInfo.GetValue(o, null) != null && propertyInfo.GetValue(o, null).ToString().IndexOf("System") < 0) ? propertyInfo.GetValue(o, null).ToString(): string.Empty);
                            }
                            catch (Exception)
                            {
                                xmlElement.AppendChild(this.xmlDocument.CreateElement(propertyInfo.Name)).InnerText =
                                    string.Empty;
                            }
                        }
                        root.AppendChild(xmlElement);
                    }
                }
                this.Save();
                opreation = true;
            }
            catch (Exception)
            {
                opreation = false;
            }
            return opreation;
        }
        #endregion
    }
}
